<?php
require_once __DIR__.'define.php';
require_once __DIR__.'/config/config.php';
require_once LIB_PATH.'function/common.php';
require_once __DIR__.'m.php';
require_once __DIR__.'c.php';
/**
 * 初始化框架
 * @author sam.shi
 *
 */
class core{
public static function init(){
		$StartTime = GetTime();
		spl_autoload_register(array('core','AutoLoader'));//开启自动加载模式
		Session::init();//开启session状态
		$GLOBALS['_setting']['debug'] ? error_reporting(E_ALL ^ E_NOTICE) : error_reporting(0);//debug状态
		date_default_timezone_set('Asia/Shanghai');//时区
		//PHP错误日志
		if($GLOBALS['_setting']['debug']){
			ini_set('error_log', DATA_PATH.'/log/php.error.log');// PHP错误记录日志
			ini_set('log_errors', '1');
		}
		// 自动转义
		if (! get_magic_quotes_gpc() && @function_exists(auto_addslashes)){
			@auto_addslashes($_POST);
			@auto_addslashes($_GET);
			@auto_addslashes($_COOKIE);
			@auto_addslashes($_REQUEST);
		}
		$mod = '';
		$act = '';

		self::UrlParse($mod, $act);//解析参数
		global $G_B;
		$G_B['modstr'] = $mod;
		$G_B['actstr'] = $act;
		
		//$G_B['template'] = $GLOBALS['_setting'][WEB_URL.'_template'];
		//print_r($G_B);exit;
		$modstr = $mod."Controller";
		$actstr = $act."Action";
		//exit($modstr);
		$language = new Language();
		$language->load();//加载语言包,必须在输出之前调用
		//$GLOBALS['_lang'] = $_lang;//将变量加载到公用变量中
        //print_r($_lang);exit;
		if(class_exists($modstr)){
			$modobj = new $modstr();
		}else{
			exit($modstr.'不存在');
		}
		$modobj->set('_lang',$GLOBALS['_lang']);
		if(method_exists($modobj,$actstr)){
			$modobj->$actstr();//加载模块操作方法
		}else{
			exit($modstr.'/'.$actstr.'不存在');
		}
		
		$EndTime = GetTime();//获取程序结束时间
		$RunTime = number_format(($EndTime - $StartTime),3);//计算运行时间
		$modobj->set('RunTime',$RunTime);
		//echo $modobj->forward;
		$modobj->display();//布局模块视图
		$LastTime= GetTime();//获取完全结束时间
		$ViewTime = number_format(($LastTime - $EndTime),3);//计算视图层运行时间
		echo '<!-- '."\r\n".'php run time:'.$RunTime."秒\r\n".'load view time:'.$ViewTime."秒\r\n -->";
	}

	/**
	 * 解析出URL地址，形如/psys/user/login
	 * string WEB_URL 子系统地址 psys,pitem,pexam,panalyse
	 * @param string $modstr 子系统模块
	 * @param string $actstr 子系统模块Action
	 */
	public static function UrlParse(&$modstr,&$actstr){
		$result_params	   = Rewrite::getParams($_SERVER['REQUEST_URI']);
		$GLOBALS['params'] = $result_params;
		//echo '<pre>';print_r($result_params);echo '</pre>';exit;
		$actstr = $GLOBALS['params']['act'];
		$modstr = $GLOBALS['params']['con'];
		//unset($GLOBALS['params']['act']);
		//unset($GLOBALS['params']['con']);
		
		$modstr = $modstr == '' ? 'index' : trim($modstr);
		$actstr = $actstr == '' ? 'index' : trim($actstr);
	}
	/*//path设置
	 protected static function set_path() {
	 set_include_path(
	 implode(PATH_SEPARATOR,
	 array(
	 LIB_PATH,
	 CONFIG_PATH,
	 get_include_path()
	 )
	 )
		);

		}*/
	//自动加载方法
	public static function AutoLoader($str_name){
            $class_name = strtolower($str_name);
            if(stripos($class_name,"controller") !== false){
                $file_name = _ROOT.'controller/'.$str_name.'.php';
            }elseif(substr($class_name, -5) == "model"){
                $file_name = _ROOT.'models/'.$str_name.'.php';
            }else{
                $file_name = LIB_PATH.'class/'.$str_name.'.php';
            }
                        
            if(file_exists($file_name)){
                if(!class_exists($str_name)){
                    return require_once($file_name);
                }
            }
	}
	
	/**
	 * 加载函数库文件
	 * @param string $name
	 */
	public static function loadFunction($name){
		$name = LIB_PATH.'function/'.$name.'.php';
		if(file_exists($name)){
			 return require_once($name);
		}
	}
	
	/**
	 * 加载类库文件
	 * @param string $name
	 */
	public static function loadClass($name){
		$name = LIB_PATH.'class/'.$name.'.php';
		if(file_exists($name)){
			 return require_once($name);
		}
	}
}